package com.lt.bookkeeping.Fragment;

import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.bumptech.glide.GenericTransitionOptions;
import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.gif.GifDrawable;
import com.bumptech.glide.request.RequestOptions;
import com.google.android.material.textfield.TextInputEditText;
import com.it.bookkeeping.greendao.BookKeepDataDao;
import com.it.bookkeeping.greendao.Dao;
import com.lt.bookkeeping.GlideApplication;
import com.lt.bookkeeping.R;
import com.lt.bookkeeping.Utils.StringUtils;
import com.lt.bookkeeping.Utils.ThreadPoolUtils;
import com.lt.bookkeeping.Utils.ToastUtils;
import com.lt.bookkeeping.dataSave.BookKeepData;

import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ThreadPoolExecutor;

import jp.wasabeef.glide.transformations.BlurTransformation;
import jp.wasabeef.glide.transformations.RoundedCornersTransformation;

/**
 * @author LiuTong
 */
public class FragmentMain extends BaseFragment {

    private TextInputEditText editTextEvent,editTextMoney;
    private TextView errEvent,errMoney;
    private Button btn;
    public FragmentMain() {

    }

    public static FragmentMain newInstance(Parcelable arg) {
        FragmentMain fragmentMain = new FragmentMain();
        Bundle argsBundle = new Bundle();
        argsBundle.putParcelable("arg",arg);
        fragmentMain.setArguments(argsBundle);
        return fragmentMain;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main_view,container,false);
        editTextEvent = view.findViewById(R.id.input_reason);
        editTextMoney = view.findViewById(R.id.input_money);
        errEvent = view.findViewById(R.id.reason_err);
        errMoney = view.findViewById(R.id.money_err);
        btn = view.findViewById(R.id.main_save_btn);
//        initEvent();
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initEvent();
    }

    private void initEvent() {
        btn.setOnClickListener(v -> {
            errEvent.setText("");
            errMoney.setText("");
            boolean isEventNull = false,isMoneyNull=false;
            if(StringUtils.isEmptyOrNull(Objects.requireNonNull(editTextEvent.getText()).toString())){
                errEvent.setText(getResources().getString(R.string.err_not_null));
                isEventNull = true;

            }
            if(StringUtils.isEmptyOrNull(Objects.requireNonNull(editTextMoney.getText()).toString())){
                errMoney.setText(getResources().getString(R.string.err_not_null));
                isMoneyNull = true;
            }

            if(isEventNull || isMoneyNull){
                return;
            }
            BookKeepData bookKeepData = new BookKeepData();
            bookKeepData.setBookKeepTime(System.currentTimeMillis());
            bookKeepData.setEvent(editTextEvent.getText().toString());
            bookKeepData.setMoney(Double.parseDouble(editTextMoney.getText().toString()));
            if(Dao.getInstance(daoSession).insert(bookKeepData)>0){
                ToastUtils.showToast(getContext(),"保存成功");
                clearEdittext();
            }
        });
    }
    private void clearEdittext(){
        editTextEvent.setText("");
        editTextMoney.setText("");
    }


    @Override
    public void onResume() {
        super.onResume();

    }

    @Override
    public void onPause() {
        super.onPause();


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onDetach() {
        super.onDetach();
    }
}
